import { Op } from 'sequelize';
import { sendResult } from '@/utils/u.sendResult';
import SiteInfoModel from '@/models/site/site_info.model';
import { NextFunction, Request, Response } from 'express';

export async function deleteHandle(
	req: Request,
	res: Response,
	next: NextFunction
): Promise<void> {
	const ids = req.query.ids as string | undefined;

	// 参数校验
	if (!ids || ids.trim() === '' || typeof ids !== 'string') {
		sendResult(res, 400, '缺少参数 `ids`');
		return;
	}

	// 格式化 ids
	const idsArr: number[] = [];
	if (ids.includes(',')) {
		// 去重
		const idsSet = [...new Set(ids.split(','))];
		idsArr.push(...idsSet.map((id) => Number(id.trim())));
	} else {
		idsArr.push(Number(ids.trim()));
	}

	try {
		// 查询删除的数据中是否有正在使用的配置信息
		const isUsing: SiteInfoModel[] = await SiteInfoModel.findAll({
			where: { id: { [Op.in]: idsArr }, isUse: 'yes' },
		});

		if (isUsing.length > 0) {
			sendResult(res, 400, '无法删除正在使用的站点信息配置');
			return;
		}

		// 删除数据
		const delResult: number = await SiteInfoModel.destroy({
			where: { id: { [Op.in]: idsArr } },
		});

		if (delResult > 0) {
			sendResult(res, 200, '删除成功');
			return;
		}
		sendResult(res, 400, '删除失败，请检查站点配置信息是否存在');
	} catch (error) {
		next(error);
	}
}
